// pages/setUp/setUp.js
const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  changeSms(e){
    let isLogin = wx.getStorageSync('isLogin')
    if (isLogin){
      this.changeStatus('smsNotice', e.detail)
    }else{
      wx.showModal({
        title: '系统提示',
        content: '您还未登录,是否去登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    }
  },
  changeWx(e){
    let isLogin = wx.getStorageSync('isLogin')
    if (isLogin){
      if (e.detail) {
        let wxOpenid = wx.getStorageSync("userInfo").wxOpenid
        this.changeStatus('wxNotice', e.detail)
        // if (wx.getStorageSync("userInfo").wxOpenid != '') {
        //   this.changeStatus('wxNotice', e.detail)
        // } else {
        //   Dialog.alert({
        //     message: '请关注“慧宝接种”微信公众号'
        //   })
        // }

      } else {
        this.changeStatus('wxNotice', false)
      }
    }else{
      wx.showModal({
        title: '系统提示',
        content: '您还未登录,是否去登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          } else if (res.cancel) {
            return
          }
        }
      })
    }
    
  },
  //跳转文章详情
  toDetail: function () {
    wx.navigateTo({
      url: '/pages/articleDetail/articleDetail'
    })
  },

  changeStatus(data,status){
    if (data =='wxNotice'){
      utils.HttpRequst(true, "/baby-cloud-system/mobileUser/updateWxNotice", "post", { status }, res => {
        if (res.data.code == 200) {
          this.setData({
            wxNotice: status
          })
          let userinfo = wx.getStorageSync("userInfo")
          userinfo.wxNotice=status
          wx.setStorageSync("userInfo", userinfo)
        }else{
          Dialog.alert({
            message: res.data.message
          })
        }
      })
    }else{
      utils.HttpRequst(true, "/baby-cloud-system/mobileUser/edit", "post", { smsNotice: status }, res => {
        if (res.data.code == 200) {
          this.setData({
            smsNotice: status
          })
          let userinfo = wx.getStorageSync("userInfo")
          userinfo.smsNotice = status
          wx.setStorageSync("userInfo", userinfo)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx.getStorageSync("userInfo").wxNotice)
    this.setData({
      wxNotice: wx.getStorageSync("userInfo").wxNotice,
      smsNotice: wx.getStorageSync("userInfo").smsNotice,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})